#ifndef QAUDIOSERVER_H
#define QAUDIOSERVER_H

#include <QObject>
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QTcpServer>
#include <QList>
#include <QTime>
#include <QTextStream>

#include "audiosink.h"
#include "user.h"
#include "readini.h"

#include <QDebug>

class AudioServer : public QObject
{
    Q_OBJECT
public:
    explicit AudioServer(QObject *parent = 0);
    ~AudioServer();
    void closeAll();
    void showAvailablesDevices();
    bool setPortStream(const int port);
    void say(const QString message);
    int getHostConnectionsCount(const QHostAddress host);
    int getHostConnectionsCount(const QString host);
    void setAllowedHosts(const QStringList hosts);
    void showUsers();
    quint64 getBufferSize();
    bool intToBool(const int x);
    static void listOutputsModules(QTextStream *out);

private:
    QTcpServer *srv;
    struct serverConfig {
        int maxClientTotalConnection;
        int maxClientConnections;
        int maxBufferSizePerClient;
        quint64 maxBufferSizeTotal;
        int bufferSize;
        int port;
        QStringList ipMasks;
        bool killAfk;
        int killAfkInterval;
        bool killBufferUnderflow;
        int killBufferUnderflowCount;
        User::userConfig defaultUserConfig;
        QString timeStamp;
        QMap<QString,User::userConfig> userConfig;
        bool showStats;
        QString configFile;
        bool dynamicConfig;
    };
    bool loadConfig(const QString filePath);
    serverConfig config;
    QTextStream *out;
    bool isValidHost(QTcpSocket *client);
    QList<User*> users;
    void loadUser(Readini *ini, const QString entry);
    void loadUser(const QString entry);
    QString getConfigFilePath();
    quint64 readedData;

signals:

public slots:
    void sockOpen();
    void sockClose(User* user);

};

#endif // QAUDIOSERVER_H
